<template>
  <v-container>
    <v-card>
      <v-card-title>要反馈的问题</v-card-title>
      <v-card-text>
        <v-text-field
          v-model="title"
          type="text"
          placeholder="标题"
        />
        <v-text-field
          v-model="content"
          prepend-icon="mdi-alert"
          placeholder="内容"
        />
        <br/>
        <v-text-field
          v-model="holder"
          type="text"
          placeholder="发起者（可不填）"
        />
      </v-card-text>
      <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn
            color="primary"
            block
            :disabled="$store.state.isLoading"
            @click="submit"
          >提交
          </v-btn>
        </v-card-actions>
    </v-card>
  </v-container>
</template>

<script>
import dialogs from "../utils/dialogs.js";
import axios from "axios";

export default {
  data: () => ({
    title: "",
    content: "",
    holder: "",
  }),
  mounted: function () {
    this.pageload();
  },
  methods: {
    pageload: async function(){
        this.$store.commit("loading",false);
    },
    getTime: function(){
        var t=new Date();
        var y=t.getFullYear();
        var m=(t.getMonth()+1)<10?('0'+(t.getMonth()+1)):(t.getMonth()+1);
        var d=t.getDate()<10?('0'+t.getDate()):t.getDate();
        return y+"-"+m+"-"+d;
    },
    submit: async function(){
        if(this.title=="" || this.title==undefined){
            dialogs.toasts.error("请填写标题");
            return;
        }
        if(this.content=="" || this.content==undefined){
            dialogs.toasts.error("请填写内容");
            return;
        }
        this.$store.commit("loading",true);
        await axios.post("/new",{
            "title": this.title,
            "content": this.content,
            "holder": this.holder,
            "time": this.getTime(),
        }).then((response)=>{
            console.log(response);
            if(response.data.type=="SUCCESS"){
                dialogs.toasts.success(response.data.message);
            }else{
                dialogs.toasts.error(response.data.message);
            }
        }).catch((err)=>{
            dialogs.toasts.error(err);
        });
        this.$store.commit("loading",false);
        this.title = "";
        this.report = "";
        this.name = "";
    }
  },
};
</script>
